package com.qianfeng.zhangwei.touchevent.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.qianfeng.zhangwei.touchevent.R;
import com.qianfeng.zhangwei.touchevent.adapter.ViewPagerAdapter;
import com.qianfeng.zhangwei.touchevent.widget.MyViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class NewFragment extends Fragment implements MyViewPager.OnSiginTouch, ViewPager.OnPageChangeListener {

    private MyViewPager viewPager;
    List<View> list;

    private ViewPagerAdapter pagerAdapter;
    private int index;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_new, container, false);
        getData();
        init(view);
        return view;
    }

    private void init(View view) {
        viewPager = (MyViewPager) view.findViewById(R.id.fragment_new_viewpager);
        viewPager.setOnSiginTouch(this);
        viewPager.addOnPageChangeListener(this);
        pagerAdapter = new ViewPagerAdapter(list);
        viewPager.setAdapter(pagerAdapter);
    }

    public void getData() {
        list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            ImageView img = new ImageView(getActivity());
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            img.setLayoutParams(params);
            img.setScaleType(ImageView.ScaleType.FIT_XY);
            switch (i) {
                case 0:
                    img.setImageResource(R.mipmap.hehe1);
                    break;
                case 1:
                    img.setImageResource(R.mipmap.hehe2);
                    break;
                case 2:
                    img.setImageResource(R.mipmap.hehe3);
                    break;
                case 3:
                    img.setImageResource(R.mipmap.hehe4);
                    break;
            }
            list.add(img);
        }

    }


    @Override
    public void onSiginTouch() {
        Toast.makeText(getActivity(), "当前选中view的id=====>>" + list.get(index).getId(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        index = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
